package com.daxia.ogmall.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 实现aop的一个例子，@Aspect和@Component要同时有。 可惜的是，似乎没法拦截controller层。
 * 
 * @date 2013-6-19
 * 
 */
@Aspect
@Component
public class TimeAOPService extends BaseControllerAOP {
	private static Logger logger = Logger.getLogger("time");
	
	private long timeout = 200;
    
    public static final String EDP = "execution(* com.daxia.ogmall.service.*.*(..))";

    public TimeAOPService() {
    }

    @Pointcut(EDP)
    public void pointcut() {
    }

    @Before("pointcut()")
    public void logBefore() {
    }

    @After("pointcut()")
    public void logAfter() {
    }

    @Around("pointcut()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    	long start = System.currentTimeMillis();
    	try {
    		return joinPoint.proceed();
    	} finally {
    		long took = System.currentTimeMillis() - start;
    		if (took > timeout) {
    			logger.error("[ " + took + " ] " + joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName());
    		}
    	}
    }
    
}
